package com.yayan.xh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.yayan.xh.bean.EnterDetailBean;

public class EnterDetailDao extends ConnectionBase implements IEnterDetailDao {
	
	private Connection conn = null;
	
	private QueryRunner qRunner = null;
	
	private PreparedStatement ps = null;
	
	private ResultSet rsKey = null;
	
	@Override
	public boolean saveEnterDetail(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "insert into tb_enterDetail(shop_id, o_insideorder, m_id, ed_name, ed_sex, ed_age, ed_address, ed_phone, ed_year, ed_status) "
					+ "values(?,?,?,?,?,?,?,?,?,0);";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("insideorder"));
			ps.setObject(3, map.get("matchId"));
			ps.setObject(4, map.get("name"));
			ps.setObject(5, map.get("sex"));
			ps.setObject(6, map.get("age"));
			ps.setObject(7, map.get("address"));
			ps.setObject(8, map.get("phone"));
			ps.setObject(9, map.get("year"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}
	
	@Override
	public boolean saveEnterDetailFront(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "insert into tb_enterDetail(shop_id, m_id, ed_name, ed_sex, ed_age, ed_address, ed_phone, ed_year, ed_status) "
					+ "values(?,?,?,?,?,?,?,?,1);";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("matchId"));
			ps.setObject(3, map.get("name"));
			ps.setObject(4, map.get("sex"));
			ps.setObject(5, map.get("age"));
			ps.setObject(6, map.get("address"));
			ps.setObject(7, map.get("phone"));
			ps.setObject(8, map.get("year"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean delEnterDetail(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			conn = getConn();
			conn.setAutoCommit(false);
			qRunner = new QueryRunner();
			String enterDetailSql = "select * from tb_enterDetail where ed_id=? and shop_id=?";
			EnterDetailBean result = qRunner.query(conn, enterDetailSql, new BeanHandler<EnterDetailBean>(EnterDetailBean.class), map.get("edId"), map.get("shopId"));
			
			String sql = "delete from tb_enterDetail where ed_id=? and shop_id=?";
			
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("edId"));
			ps.setObject(2, map.get("shopId"));
			boolean ef = ps.executeUpdate() > 0 ? true : false;
			
			if(ef) {
				String enterSql = "delete from tb_enter where e_id=? and shop_id=?";
				ps = conn.prepareStatement(enterSql);
				ps.setObject(1, result.getE_id());
				ps.setObject(2, map.get("shopId"));
				f = ps.executeUpdate() > 0 ? true : false;
				conn.commit();
				conn.setAutoCommit(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} close(conn, ps, rsKey);
		return f;
	}

	@Override
	public boolean modifyEnterDetail(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "update tb_enterDetail set e_id=?, ed_status=1 where o_insideorder=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("eId"));
			ps.setObject(2, map.get("insideorder"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public List<EnterDetailBean> getEnterDetailByMId(Map<String, String> map) throws Exception {
		List<EnterDetailBean> result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_enterDetail where shop_id=? and m_id=? and ed_status=1";
			conn = getConn();
			result =  qRunner.query(conn, sql, new BeanListHandler<EnterDetailBean>(EnterDetailBean.class), map.get("shopId"), map.get("matchId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public EnterDetailBean getEnterDetailByOrderId(Map<String, String> map) throws Exception {
		EnterDetailBean result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_enterDetail where o_insideorder=?";
			conn = getConn();
			result =  qRunner.query(conn, sql, new BeanHandler<EnterDetailBean>(EnterDetailBean.class), map.get("insideorder"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

}
